import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/views/Layout'
import store from '@/store'
Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/layout'
},
{
  path: '/login',
  component: () => import('@/views/Login')
}, {
  path: '/layout',
  name: 'Layout',
  component: Layout,
  redirect: '/layout/home',
  children: [
    {
      path: 'home',
      component: () => import('@/views/Home'), // -------------------路由懒加载
      meta: { // 路由对象里信息(额外参数)
        isScoll: true,
        top: 0
      }
    },
    {
      path: 'user',
      component: () => import('@/views/User')
    }
  ]
},
{
  path: '/search',
  component: () => import('@/views/Search')
}, {
  path: '/search/:kw',
  component: () => import('@/views/Search/SearchResult')
}, {
  path: '/article',
  component: () => import('@/views/ArticleDetail')
}, {
  path: '/user/edit',
  component: () => import('@/views/User/UserEditor')
},
{
  path: '/chat',
  component: () => import('@/views/Chat')
}
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 有token, 不能去登录页
  // 无token, 需要用户"权限"的才需要去登录页
  if (store.state.token.length > 0 && to.path === '/login') {
    // 证明有token-已经登录了
    next(false) // 阻止跳转原地呆着
  } else {
    next()
  }
})
export default router
